package com.wideplay.warp.remoting;

import com.google.inject.Key;

import java.lang.annotation.Annotation;

/**
 * Created by IntelliJ IDEA.
 * User: dprasanna
 * Date: 1/10/2007
 * Time: 15:46:35
 * <p/>
 *
 * Binds an interface to an implementation and scope. Both client and server side.
 *
 * TODO Need to validate if a scope was specified.
 *
 * @author dprasanna
 * @since 1.0
 */
class RemotingModuleBuilderImpl<T> implements RemoteLinkedBindingBuilder<T> {
    private final Class<T> iface;
    private Key<? extends T> implementationKey;
    private AbstractRemotingModule module;

    public RemotingModuleBuilderImpl(Class<T> iface, AbstractRemotingModule remoteBindings) {
        this.iface = iface;
        this.module = remoteBindings;
    }

    public RemoteScopeBindingBuilder to(Class<? extends T> clazz) {
        //resolve key from class
        this.implementationKey = Key.get(clazz);

        return this;
    }

    public RemoteScopeBindingBuilder to(Key<? extends T> key) {
        this.implementationKey = key;

        return this;
    }

    public void in(Class<? extends Annotation> remoteScope) {
        //add the binding

        module.doRemotedBinding(new RemoteBinding<T>(iface, implementationKey, remoteScope));
    }

}
